//
//  MyMenuViewController.m
//  ForengnMenu
//
//  Created by @-@ on 16/1/14.
//  Copyright © 2016年 余旺. All rights reserved.
//

#import "MyMenuViewController.h"
#import "MyMenuTableViewCell.h"
#import "MenuContentViewController.h"

@interface MyMenuViewController ()

@property NSMutableArray * recipeArray;

@property (weak, nonatomic) IBOutlet UITableView *myTableView;


@end

@implementation MyMenuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (_isOk) {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:@"Icon_return"] forState:UIControlStateNormal];
        button.frame = CGRectMake(0, 0, 50, 30);
        [button addTarget:self action:@selector(returnAction) forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    }else{
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:@"headImage.jpg"] forState:UIControlStateNormal];
        button.frame = CGRectMake(0, 0, 30, 30);
        button.layer.cornerRadius = button.frame.size.height/2.0f;
        button.layer.masksToBounds = YES;
        
        [button addTarget:self action:@selector(headAction) forControlEvents:UIControlEventTouchUpInside];
        
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    }

    self.myTableView.backgroundColor =[UIColor colorWithRed:246/255.0f green:247/255.0f blue:239/255.0f alpha:1.0f];
    
    self.myTableView.separatorStyle = UITableViewCellStyleDefault;
    
    //指定的位置
    NSString * RecipeModel = [[NSBundle mainBundle] pathForResource:@"RecipeModel" ofType:@"plist"];
    //大数组
    NSArray * array = [NSArray arrayWithContentsOfFile:RecipeModel];

    self.recipeArray = [array[1] mutableCopy];
   
    if (_isOk) {
       [self.recipeArray insertObject:self.chuan atIndex:0]; 
    }
    
    
}



-(void)returnAction{

    [self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.recipeArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * identifier = @"Cell";
    
    MyMenuTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    NSDictionary * array = self.recipeArray[indexPath.section];
    
    NSDictionary * dicArr = array[@"Content"];
    
    cell.foodImageView.image = [UIImage imageNamed:dicArr[@"CookingImage"]];
    if (!cell.foodImageView.image) {
        cell.foodImageView.image = dicArr[@"CookingImageData"];
    }
    
    cell.myTextLabel.text = dicArr[@"RecipeName"];
    
    cell.diffcultLabel.text = dicArr[@"CookingDifficulty"];
    
    cell.timeLabel.text = dicArr[@"CookingTime"];
    
    cell.myFuBiaoTi.text =dicArr[@"RecipeShow"];
    
    cell.backgroundColor = [UIColor clearColor];
    
    return cell;

}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return 14;
    }else{
        return 12;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}
- (void)headAction{
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushSlideslip" object:nil];

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary * array = self.recipeArray[indexPath.section];
    
    UIStoryboard * stroyboard = [UIStoryboard storyboardWithName:@"LearnCook" bundle:[NSBundle mainBundle]];
    
    MenuContentViewController * viewController = [stroyboard instantiateViewControllerWithIdentifier:@"MenuController"];
    viewController.recipeArray = array;
    viewController.isEnter = YES;
    
    [self.navigationController pushViewController:viewController animated:YES];
    

}


@end
